/**
 * File: SimpleProcessableCollection.java
 * Created by: mhaimel
 * Created on: Mar 30, 2010
 * CVS:  $Id: SimpleProcessableCollection.java 1.0 Mar 30, 2010 2:13:25 PM mhaimel Exp $
 */
package uk.ac.ebi.curtain.model.graph.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import uk.ac.ebi.curtain.model.graph.ProcessableCollection;
import uk.ac.ebi.curtain.model.graph.GraphAccess.ProcessEach;
import uk.ac.ebi.curtain.model.graph.GraphAccess.TouchEach;

/**
 * @author mhaimel
 *
 */
public class SimpleProcessableCollection<E> implements ProcessableCollection<E>, TouchEach<E> {
	
	private final LinkedBlockingQueue<E> list;
	
	public SimpleProcessableCollection() {
		this.list = new LinkedBlockingQueue<E>();
	}
	
	public SimpleProcessableCollection(Collection<E> coll) {
		this.list = new LinkedBlockingQueue<E>(coll);
	}	

	@Override
	public ProcessableCollection<E> processQueue(ProcessEach<E> filter) {
		SimpleProcessableCollection<E> newList = new SimpleProcessableCollection<E>();
		int i = 0;
		for(E element : list){
			E newElement = filter.process(i, element);
			if(null != newElement){
				newList.add(newElement);
			}
			++i;
		}
		return newList;
	}

	@Override
	public ProcessableCollection<E> processQueue(TouchEach<E> touch) {
		int i = 0;
		for(E element : list){
			touch.touch(element);
			++i;
		}
		return this;
	}
	

	@Override
	public <T extends ProcessEach<E>> T process(T filter) {
		int i = 0;
		for(E element : list){
			filter.process(i, element);
		}
		return filter;
	}

	@Override
	public <T extends TouchEach<E>> T process(T touch) {
		int i = 0;
		for(E element : list){
			touch.touch(element);
			++i;
		}
		return touch;
	}

	@Override
	public boolean add(E e) {
		return this.list.add(e);
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		return this.list.addAll(c);
	}

	@Override
	public void clear() {
		this.list.clear();
	}

	@Override
	public boolean contains(Object o) {
		return this.list.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return this.list.containsAll(c);
	}

	@Override
	public boolean isEmpty() {
		return this.list.isEmpty();
	}

	@Override
	public Iterator<E> iterator() {
		return this.list.iterator();
	}

	@Override
	public boolean remove(Object o) {
		return this.list.remove(o);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return this.list.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return this.list.retainAll(c);
	}

	@Override
	public int size() {
		return this.list.size();
	}

	@Override
	public Object[] toArray() {
		return this.list.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return this.list.toArray(a);
	}
	
	public List<E> toList() {
		return new ArrayList<E>(this.list);
	}

	@Override
	public void touch(E element) {
		if(null != element){
			this.add(element);
		}
	}

}
